package com.util;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;


@WebListener
public class OnlineUserNumberListener implements HttpSessionListener, HttpSessionAttributeListener{

	private int num; 

	public OnlineUserNumberListener() {

	}


	public void sessionCreated(HttpSessionEvent se) {
		
		num++;
		ServletContext context = se.getSession().getServletContext();
		
		context.setAttribute("onlineUserNum", num);

	}


	public void sessionDestroyed(HttpSessionEvent se) {
		
		num--;
		ServletContext context = se.getSession().getServletContext();
		
		context.setAttribute("onlineUserNum", num);
	}

	@Override
	public void attributeAdded(HttpSessionBindingEvent arg0) {
		System.out.println("attributeAdded");
		
	}

	@Override
	public void attributeRemoved(HttpSessionBindingEvent arg0) {
		System.out.println("attributeRemoved");
		
	}

	@Override
	public void attributeReplaced(HttpSessionBindingEvent arg0) {
		System.out.println("attributeReplaced");
		
	}

}
